<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 86400);
               
// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
$app->initialise();

require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'reportProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'pointProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'PHPExcel.php';

// Init excel
$objPHPExcel = new PHPExcel();
// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->mergeCells('A1:L1');
$activeSheet->setCellValue('A1', 'Complain Report');
$activeSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle('A1')->getFont()->setSize(24);
$activeSheet->getStyle('A1')->getFont()->setBold(true);

/************** FILL HEADER **************/
$index = 5;
$activeSheet->setCellValue('A'.$index, 'Member ID');
$activeSheet->setCellValue('B'.$index, 'Name');
$activeSheet->setCellValue('C'.$index, 'Phone');
$activeSheet->setCellValue('D'.$index, 'Created Date');
$activeSheet->setCellValue('E'.$index, 'Complaint');
$activeSheet->setCellValue('F'.$index, 'Action Taken');
$activeSheet->setCellValue('G'.$index, 'Complaint Type');
$activeSheet->setCellValue('H'.$index, 'Meal period');
$activeSheet->setCellValue('I'.$index, 'Outlet');
$activeSheet->setCellValue('J'.$index, 'Visit Date');
$activeSheet->setCellValue('K'.$index, 'Handle By');
$activeSheet->setCellValue('L'.$index, 'Guest Type');
$activeSheet->setCellValue('M'.$index, 'Table No');
$fill = $activeSheet->getStyle('A5:M5')->getFill();
$fill->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$fill->getStartColor()->setARGB('cc99ff');

$cols = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M');

foreach($cols as $item)
{
    if($item == 'B')
    {
        setBackgroundColor($activeSheet, $item, 5, 35);
    }
    else if($item == 'E' || $item == 'F' || $item == 'I')
    {
        setBackgroundColor($activeSheet, $item, 5, 50);
    }
    else if($item == 'D')
    {
        setBackgroundColor($activeSheet, $item, 5, 28);
    }
    else
    {
        setBackgroundColor($activeSheet, $item, 5, 18);   
    }
}

// End init excel

/******** Query Data ********/  
$db = JFactory::getDbo();
$conditions = array();
$conditions['filter_field'] = strval($_GET['ff']);
$conditions['search_value'] = strval($_GET['sv']);
$conditions['type'] = strval($_GET['t']);
$conditions['meal'] = strval($_GET['m']);
$conditions['dateFrom'] = strval($_GET['df']);
$conditions['dateTo'] = strval($_GET['dt']);
$conditions['outlet'] = strval($_GET['o']);
$conditions['handle_by'] = strval($_GET['h']);

$data = reportProvider::complainReport($conditions, $db, intval($_GET['l']));


if(!empty($data))
{
    $activeSheet->mergeCells('A3:J3');
    $activeSheet->setCellValue('A3', 'Total: '.$data['total'].'. Display top: '.count($data['items']));   
    $style = $activeSheet->getStyle('A3')->getFont();
    $style->setSize(16);
    $style->setBold(true);
    $style->setColor( new PHPExcel_Style_Color( 'CC0000' ) );
    
    /*********** BIND TO EXCEL ***********/
    $index = 6;
    $data = $data['items'];
    $size = count($data);
    //$complainType = JHTMLwrd::listComplainTypeRaw();
	$complainType = array(
        '1' => JText::_('Food'),
        '2' => JText::_('Beverage'),
        '3' => JText::_('Sevice'),
        '4' => JText::_('Other')
    );
    $mealPeriod = array(
        '1' => JText::_('Breakfast'),
        '2' => JText::_('Lunch'),
        '3' => JText::_('Dinner')
    );
    
    for($i=0; $i<$size; $i++)
    {
        $activeSheet->setCellValue('A'.$index, 'V'.$data[$i]->vip_number); 
        $activeSheet->setCellValue('B'.$index, $data[$i]->l_name.' '.$data[$i]->f_name);    
        
		$activeSheet->setCellValue('C'.$index, $data[$i]->phone); 
		
		$activeSheet->setCellValue('D'.$index, $data[$i]->created_date);                
        $activeSheet->getStyle('D'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
       
	    $activeSheet->setCellValue('E'.$index, $data[$i]->complain);             
        $activeSheet->setCellValue('F'.$index, $data[$i]->action_taken);                
           
        
                 
        $activeSheet->setCellValue('G'.$index, $complainType[$data[$i]->complain_type]);            
        $activeSheet->setCellValue('H'.$index, $mealPeriod[$data[$i]->meal_period]);            
        $activeSheet->setCellValue('I'.$index, $data[$i]->outlet);           
         
        $activeSheet->setCellValue('J'.$index, $data[$i]->visit_date);            
        $activeSheet->getStyle('J'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $activeSheet->setCellValue('K'.$index, $data[$i]->handle_by);            
        $activeSheet->getStyle('K'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
		$activeSheet->setCellValue('L'.$index, $data[$i]->guess_type);
        $activeSheet->setCellValue('M'.$index, $data[$i]->table_no);
		$index++;
    }
    unset($data);
}

$fileName = 'complain_report_'.date('m-d-Y_H_i_s');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(JPATH_BASE.DS.'tmp'.DS.$fileName.'.xlsx');

$fullPath = JPATH_ROOT.DS.'tmp'.DS.$fileName.'.xlsx';
$fsize = filesize($fullPath);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $fsize);
ob_clean();
flush();
readfile( $fullPath );
exit;          


function setBackgroundColor(&$activeSheet, $colName, $colIndex, $width=15)
{
    $activeSheet->getColumnDimension($colName)->setWidth($width);    
    
    $style = $activeSheet->getStyle($colName.$colIndex);
    $style->getFont()->setSize(12);
    //$style->getFont()->setBold(true);
    $style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
